import { Module } from 'vuex';
import { Session } from '@/utils/storage';
import { UserInfosState, RootStateTypes, UserInfos } from '@/store/interface';

const userInfosModule: Module<UserInfosState, RootStateTypes> = {
	namespaced: true,
	state: {
		userInfos: {
			photo:
				'https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=1813762643,1914315241&fm=26&gp=0.jpg',
			userName: '',
			authBtnList: [],
			authPageList: [],
		},
	},
	mutations: {
		// 设置用户信息
		getUserInfos(state, data: UserInfos) {
			state.userInfos = data;
		},
	},
	actions: {
		// 设置用户信息
		async setUserInfos({ commit }, data: UserInfos) {
			if (data) {
				commit('getUserInfos', data);
			} else {
				if (Session.get('userInfo')) commit('getUserInfos', Session.get('userInfo'));
			}
		},
	},
};

export default userInfosModule;
